package com.smartleanx.module.hrm.app.risk;

import cn.hutool.core.collection.CollUtil;
import com.smartleanx.framework.datapermission.core.annotation.DataPermission;
import com.smartleanx.module.hrm.dal.dataobject.companysystem.CompanySystemDO;
import com.smartleanx.module.hrm.service.companysystem.CompanySystemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 风险点全链路检出执行器
 *
 * @author tengcj
 * @since 2024/6/6
 */
@Slf4j
@Component
public class RiskHandleChainService implements ApplicationContextAware {

    List<RiskHandle> handleList = new ArrayList<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, RiskHandle> serviceMap = applicationContext.getBeansOfType(RiskHandle.class);
        handleList = serviceMap.values().stream().sorted(Comparator.comparing(RiskHandle::type)).collect(Collectors.toList());
    }

    @DataPermission(enable = false)
    @Transactional(rollbackFor = Exception.class)
    public void execute(Set<Long> recordIds) {
        if (CollUtil.isEmpty(recordIds)) {
            return;
        }
        for (RiskHandle riskHandle : handleList) {
            riskHandle.handleRisk(recordIds);
        }
    }

}
